/*
 * BaseMediaChooserActivity.java
 * classes : com.ruint.lib.mediachooser.activity.BaseMediaChooserActivity
 * @author ruint
 * V 1.0.0
 * Create at 2014-12-4 上午11:29:18
 */
package com.ruint.lib.mediachooser.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.ruint.core.adapter.BaseListAdapter;
import com.ruint.core.app.BaseActivity;
import com.ruint.core.utils.ui.ActionSheetUtils;
import com.ruint.core.view.dialog.ActionSheet;
import com.ruint.core.view.dialog.ActionSheet.ActionSheetListener;
import com.ruint.corelib.R;
import com.ruint.lib.mediachooser.bean.ImageItem;
import com.ruint.lib.mediachooser.core.MediaChooser;
import com.ruint.lib.mediachooser.core.MediaChooserConstants;
import com.ruint.lib.mediachooser.receiver.ImageResultReceiver;
import com.ruint.lib.mediachooser.receiver.VideoResultReceiver;

/**
 * com.ruint.lib.mediachooser.activity.BaseMediaChooserActivity
 * 
 * @author ruint <br/>
 *         create at 2014-12-4 上午11:29:18
 */
public abstract class BaseMediaChooserActivity<T extends BaseListAdapter<String>> extends BaseActivity implements
    OnItemClickListener, ActionSheetListener {
  protected GridView mGridView;
  protected T mAdapter;
  protected List<ImageItem> mImageItems;
  protected List<String> mSelected;

  private ImageResultReceiver<BaseListAdapter<String>> imageReceiver;
  private VideoResultReceiver<BaseListAdapter<String>> videoReceiver;

  @Override
  protected void pre4Create(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
      mSelected = savedInstanceState.getStringArrayList(MediaChooserConstants.MEDIA_SELECTED_LIST);
    }
  }

  @Override
  protected void findViews() {
    mGridView = findView(mGridView, getGridViewID());
    onInitView();
  }

  @Override
  protected void onLoadingData() {
    mAdapter = getAdapter();
    mGridView.setAdapter(mAdapter);
    mSelected = mAdapter.getList();
    if (mSelected != null) {
      mAdapter.refresh(mSelected);
    }
    mGridView.setOnItemClickListener(this);
    onLoadData();
    onRegister();
    setMediaChooseLimit();
  }

  private void setMediaChooseLimit() {
    MediaChooser.setSelectionLimit(getMediaChooseLimit());
  }

  protected abstract void onLoadData();

  protected abstract void onInitView();

  protected abstract int getGridViewID();

  protected abstract T getAdapter();

  protected abstract int getMediaChooseLimit();

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    if (mAdapter != null && mAdapter.getList() != null) {
      outState.putStringArrayList(MediaChooserConstants.MEDIA_SELECTED_LIST, (ArrayList<String>) mAdapter.getList());
    }
    super.onSaveInstanceState(outState);
  }

  private void onRegister() {
    imageReceiver = new ImageResultReceiver<BaseListAdapter<String>>(mAdapter);
    registerReceiver(imageReceiver, MediaChooserConstants.getImageRecvFilter());
    videoReceiver = new VideoResultReceiver<BaseListAdapter<String>>(mAdapter);
    registerReceiver(videoReceiver, MediaChooserConstants.getVideoRecvFilter());
  }

  protected List<ImageItem> getImageItemList() {
    mImageItems = new ArrayList<ImageItem>();
    if (mAdapter.getList() != null && mAdapter.getList().size() > 0) {
      for (int i = 0; i < mAdapter.getList().size(); i++) {
        mImageItems.add(new ImageItem(mAdapter.getItem(i), new File(mAdapter.getItem(i))));
      }
    }
    return mImageItems;
  }

  /**
   * invoke by {@link #onItemClick(AdapterView, View, int, long)}
   * {@code if (position < MediaChooserConstants.MAX_MEDIA_LIMIT)}
   */
  protected abstract void viewPhoto();

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (position < MediaChooserConstants.SELECTED_MEDIA_COUNT) {
      viewPhoto();
    } else {
      ActionSheetUtils.showActionSheet(thisInstance, getSupportFragmentManager(), new String[] { "Folders", "Files" },
          getString(R.string.cancel), true, this);
    }
  }

  @Override
  public void onDismiss(ActionSheet actionSheet, boolean isCancel) {

  }

  @Override
  public void onOtherButtonClick(ActionSheet actionSheet, int index) {
    startMediaChooser(index);
  }

  private void startMediaChooser(int index) {
    switch (index) {
      case 0:
        startActivity(new Intent(thisInstance, BucketViewActivity.class));
        break;
      case 1:
        startActivity(new Intent(thisInstance, FileViewActivity.class));
        break;
    }
  }

  @Override
  protected void onDestroy() {
    unregisterReceiver(imageReceiver);
    unregisterReceiver(videoReceiver);
    clearConstants();
    super.onDestroy();
  }

  private void clearConstants() {
    MediaChooserConstants.clear();
  }
}
